﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EF4.Services;

using System.IO;
using EF4.Model.Entities;
using EF4.Core.Logger;

namespace EF4.UI.MVC.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        ProductService productService = null;
        public HomeController()
        {
        }
        //public HomeController(IProductService productService)
        //{
        //    this.productService = (ProductService)productService;

        //}

        public ActionResult Index()
        {
            ViewData["Message"] = "Welcome to ASP.NET MVC!";
            Product p = (new ProductService()).GetById(1);

            return RedirectToAction("Test");
        }

        public ActionResult About()
        {
            return View();
        }
        public ActionResult Test()
        {
            
            ViewData["Testcase"] = new CategoryService().GetByPK("Id", 1);
            ViewData.Model = new ProductService().AllItems().AsEnumerable();
            return View();
        }
        public ActionResult Paging(int? page, int? pagesize)
        {
            ViewData["Testcase"] = new CategoryService().GetByPK("Id", 1);
            ViewData.Model = new ProductService().GetListPaged(page.HasValue ? page.Value : 0, pagesize.HasValue ? pagesize.Value : 5, "CreatedDate", Core.OrderDirections.DESC);
            return View();
        }
        public ActionResult Add()
        {
            Category cat = new CategoryService().GetById(1);
            string name = string.Format("{0} - {1} ", "Product", cat.Products.Count);
           
            new ProductService().Add(new Model.Entities.Product()
            {
                CreatedDate =DateTime.Now,
                Alias=name,
                Name = name,
                Status = "Activate",
                UpdatedDate = DateTime.Now,
                Category =cat

            });
            Logger.Info(DateTime.Now.ToString());
            return RedirectToAction("Test");
        }
        public ActionResult Delete(int id)
        {
            Product product  = new ProductService().GetById(id);

            new ProductService().Delete(product);
            return RedirectToAction("Test");
        }
        public ActionResult Update(int id)
        {
            Product product = new ProductService().GetById(id);
            product.CreatedDate = DateTime.Now;
            
            new ProductService().Update(product);
            return RedirectToAction("Test");
        }

    }
}
